package com.saicent.note.core.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.saicent.note.core.Article;
import com.saicent.note.core.ArticleLoader;
import com.saicent.note.core.RegularExpression;
import com.saicent.support.utils.StringUtils;

public abstract class AbstractArticleLoader implements ArticleLoader {

	private String workDirectory;
	private Log log = LogFactory.getLog(getClass());
	
	protected List<Article> loadArticleFromDirectory(String directory) {
		List<Article> articles = new ArrayList<>();
		File articleDirectory = new File(workDirectory, directory);
		if (!articleDirectory.exists() || !articleDirectory.isDirectory()) {
			return null;
		}
		File[] articleFiles = articleDirectory.listFiles(new FileFilter() {
			@Override
			public boolean accept(File pathname) {
				return pathname.getName().endsWith(".jsp");
			}
		});
		for (File articleFile : articleFiles) {
			Article article = readArticleFile(articleFile);
			if (article != null) {
				debug(article);
				articles.add(article);
			}
		}
		return articles;
	}
	
	private String getTitle(String line) {
		Matcher matcher = RegularExpression.titleElement.matcher(line);
		if (matcher.find()) {
			return matcher.group(1);
		}
		return null;
	}
	
	private String getCategories(String line) {
		Matcher matcher = RegularExpression.metaElement.matcher(line);
		if (matcher.find()) {
			return matcher.group(1);
		}
		return null;
	}
	
	private Article readArticleFile (File file) {
		Article article = null;
		try (InputStream inputStream = new FileInputStream(file)) {
			Reader reader = new InputStreamReader(inputStream, "UTF8");
			BufferedReader bufferedReader = new BufferedReader(reader);
			String line = null;
			boolean titleIsFinded = false, categoriesIsFinded = false;
			String title = null, categories = null;
			while ((line = bufferedReader.readLine()) != null) {
				if (!titleIsFinded && (title = getTitle(line)) != null) {
					titleIsFinded = true;
				}
				if (!categoriesIsFinded && (categories = getCategories(line)) != null) {
					categoriesIsFinded = true;
				}
				if (titleIsFinded && categoriesIsFinded) {
					break;
				}
			}
			if (StringUtils.isNoneBlank(title, categories)) {
				return Article.initArticle(title, 
						categories, file.getCanonicalPath().replace(workDirectory, ""));
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw new RuntimeException(e);
		}
		return article;
	}
	
	private void debug(Article article) {
		log.info("<<" + article.getTitle() + ">>");
	}
	
	@Override
	public void setWorkDirectory(String workDirectory) {
		this.workDirectory = workDirectory;
	}
	
	public static void main(String[] args) throws Exception {
		
	}
}
